package com.h1.staffmanage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.h1.commonutils.R;
import com.h1.staffmanage.entity.Recruitment;
import com.h1.staffmanage.service.RecruitmentService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mengfanyu
 * @since 2022-06-22
 */
@RestController
@CrossOrigin
@RequestMapping("/staffmanage/recruitment")
public class RecruitmentController {
    @Autowired
    private RecruitmentService recruitmentService;

    @GetMapping("findAllRecruitment")
    public R findAllTeacher() {
        //调用service的方法实现查询所有的操作
        List<Recruitment> list = recruitmentService.list(null);
        return R.ok().data("items", list);
    }

    @DeleteMapping("{id}")
    public R removeTeacher(
            @PathVariable long id) {
        boolean flag = recruitmentService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("pageRecruitment/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit) {
        //创建page对象
        Page<Recruitment> pageRT = new Page<>(current, limit);


        recruitmentService.page(pageRT, null);

        long total = pageRT.getTotal();//总记录数
        List<Recruitment> records = pageRT.getRecords(); //数据list集合


        return R.ok().data("total", total).data("rows", records);
    }

    //添加讲师接口的方法
    @PostMapping("addRecruitment")
    public R addTeacher(@RequestBody Recruitment recruitment) {
        boolean save = recruitmentService.save(recruitment);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("getRecruitment/{id}")
    public R getTeacher(@PathVariable long id) {
        Recruitment byId = recruitmentService.getById(id);
        return R.ok().data("recruitmentById", byId);
    }

    @PostMapping("updateRecruitment")
    public R updateTeacher(@RequestBody Recruitment recruitment) {
        boolean flag = recruitmentService.updateById(recruitment);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

